package com.icehand.demo.controller;

import com.icehand.demo.model.User;
import com.icehand.demo.service.DemoService;
import com.icehand.redis.exception.IcehandRedisException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author icehand
 */
@RestController
@RequestMapping("/demo")
public class DemoController {
    private final DemoService demoService;

    @Autowired
    public DemoController(DemoService demoService) {
        this.demoService = demoService;
    }

    @GetMapping("/cache/annotation")
    public User cacheAnnotation(){
        return demoService.cacheAnnotation();
    }

    @GetMapping("/cache/template")
    public User cacheTemplate(){
        return demoService.cacheTemplate();
    }

    @GetMapping("/cache/lock")
    public String cacheLock(){
        User user = new User().setId(1L).setName("xxx");
        try{
            return demoService.cacheLock(user);
        }catch (IcehandRedisException e){
            return "fail";
        }
    }

    @GetMapping("/bf/{key}")
    public Boolean bf(@PathVariable("key")String key){
        return demoService.putBloomFilter(key);
    }
}